package com.moehu.back.controller.app;

import com.moehu.back.annotation.AppRestController;
import com.moehu.back.annotation.UserLoginToken;
import com.moehu.back.model.OpusModel;
import com.moehu.back.model.PagingModel;
import com.moehu.back.model.PushOpusModel;
import com.moehu.back.model.response.OpusInfoResponseModel;
import com.moehu.back.model.response.PagingResponseModel;
import com.moehu.back.model.ResultModel;
import com.moehu.back.model.response.RankingResponseModel;
import com.moehu.back.service.impl.OpusServiceImpl;
import com.moehu.back.utils.IpUtil;
import com.moehu.back.utils.Tool;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/opus")
@AppRestController
@Api(tags = "Opus 作品相关的接口")
public class OpusController {


    @Autowired
    OpusServiceImpl opusService;


    @ApiOperation(value="获取【推荐】的作品", notes="获取首页【推荐】的作品数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
    })
    @PostMapping("/Recommend")
    public ResultModel<PagingResponseModel<List<OpusModel>>> RecommendOpus(@RequestBody PagingModel pagingModel) {
        if (pagingModel.getCount()==null) { pagingModel.setCount(15); }
        if (pagingModel.getPage()==null) { pagingModel.setPage(1); }
        return opusService.OpusList(pagingModel);
    }


    @UserLoginToken
    @ApiOperation(value="【需要Token】获取【关注】的作品", notes="获取首页用户【关注】的作品数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/Follow")
    public ResultModel<PagingResponseModel<List<OpusModel>>> FollowOpus(@RequestBody PagingModel pagingModel) {
        if (pagingModel.getCount()==null) {
            pagingModel.setCount(15);
        }

        if (pagingModel.getPage()==null) {
            pagingModel.setPage(1);
        }

        return opusService.FollowOpusList(pagingModel);
    }



    @ApiOperation(value="获取【排行】的作品", notes="获取首页用户【排行】的作品数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/Ranking")
    public ResultModel<PagingResponseModel<List<RankingResponseModel>>> RankingOpus(@RequestBody PagingModel pagingModel) {
        if (pagingModel.getCount()==null) {
            pagingModel.setCount(15);
        }

        if (pagingModel.getPage()==null) {
            pagingModel.setPage(1);
        }

        return opusService.RankingOpusList(pagingModel);
    }


    @UserLoginToken
    @ApiOperation(value="【需要Token】关注和取消关注接口", notes="如果用户没有被关注，那么调用本接口就会关注用户，反之如果已经关注过了，那么就会取消关注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_to_id", value = "被关注用户的user_id", required = true, dataType = "Number")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/FollowUser")
    public ResultModel FollowUser(@RequestBody Map<String, Integer> User) {
        return opusService.addFollowUser(User.get("user_to_id"));
    }


    @UserLoginToken
    @ApiOperation(value="【需要Token】检查当前用户是否被关注", notes="传入被关注的用户user_to_id，如果被关注了返回 true，如果没有被关注那么返回 false")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_to_id", value = "被关注用户的user_id", required = true, dataType = "Number")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/CheckUserIsFollow")
    public ResultModel CheckUserIsFollow(@RequestBody Map<String, Integer> User) {
        return opusService.checkUserIsFollow(User.get("user_to_id"));
    }


    @ApiOperation(value="作品详情", notes="查看某个作品的详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "作品的id", required = true, dataType = "Number")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/OpusInfo")
    public ResultModel<OpusInfoResponseModel> OpusInfo(@RequestBody Map<String, Integer> Opus) {
        return opusService.getOpusInfo(Opus.get("opus_id"), "app");
    }

    @UserLoginToken
    @ApiOperation(value="【需要Token】收藏 & 取消收藏 作品", notes="如果作品未被收藏，那么进行收藏。如果收藏过了，那么调用该接口就取消收藏！")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "被收藏的作品id", required = true, dataType = "Number")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/CollectionOpus")
    public ResultModel collectionOpus(@RequestBody Map<String, Integer> Opus) {
        return opusService.collectionOpus(Opus.get("opus_id"));
    }


    @ApiOperation(value="获取用户收藏的作品列表", notes="个人空间获取收藏列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
            @ApiImplicitParam(name = "userid", value = "用户的user_id", required = true, dataType = "Number", defaultValue=""),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/CollectionOpusList")
    public ResultModel<PagingResponseModel<List<OpusModel>>> collectionOpusList(@RequestBody PagingModel pagingModel) {
        if (pagingModel.getCount()==null) { pagingModel.setCount(15); }
        if (pagingModel.getPage()==null) { pagingModel.setPage(1); }
        return opusService.collectionOpusList(pagingModel);
    }

    @ApiOperation(value="获取用户收藏作品的详情", notes="点击收藏列表的图片，获取详情数据（只返回作品的图片）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "作品的id", required = true, dataType = "Number", defaultValue="1"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/CollectionOpusInfo")
    public ResultModel collectionOpusInfo(@RequestBody Map<String, Integer> Opus) {
        return opusService.collectionOpusInfo(Opus.get("opus_id"));
    }



    @ApiOperation(value="个人空间获取投稿", notes="个人空间获取投稿")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
            @ApiImplicitParam(name = "userid", value = "用户的user_id", required = true, dataType = "Number", defaultValue=""),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/OpusList")
    public ResultModel MyOpusList(@RequestBody PagingModel user) {
        return opusService.MyOpusList(user);
    }



    @UserLoginToken
    @ApiOperation(value="【需要Token】点赞 & 取消点赞接口", notes="对作品 & 动态进行点赞 和 取消点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "作品的id，如果点赞动态那么这里为空字符串：'' ", required = true, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "dynamic_id", value = "动态的id，如果点赞作品那么这里为空字符串：''", required = true, dataType = "Number", defaultValue="1"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/UpvoteOpus")
    public ResultModel upvoteOpus(@RequestBody Map<String, String> Opus) {
        return opusService.upvoteOpus(Opus);
    }

    @UserLoginToken
    @ApiOperation(value="【需要Token】检查作品 & 动态是否被点赞", notes="主要用于进入详情页后检查作品 & 动态 是否被点赞，true表示点赞过了，false 表示未被点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "作品的id，如果点赞动态那么这里为空字符串：'' ", required = true, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "dynamic_id", value = "动态的id，如果点赞作品那么这里为空字符串：''", required = true, dataType = "Number", defaultValue="1"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/CheckIsUpvote")
    public ResultModel checkOpusIsUpvote(@RequestBody Map<String, String> Opus) {
        return opusService.checkOpusIsUpvote(Opus.get("opus_id"), Opus.get("dynamic_id"));
    }


    @UserLoginToken
    @ApiOperation(value="【需要Token】发布作品", notes="用户发布作品接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_title", value = "作品的标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "opus_introduce", value = "作品的介绍", required = true, dataType = "String"),
            @ApiImplicitParam(name = "opus_tags", value = "作品的标签数组，例如：['娱乐','大雄','小夫']", required = true, dataType = "Array"),
            @ApiImplicitParam(name = "opus_imgs", value = "作品的图片数组，例如：['地址1','地址2','地址3']", required = true, dataType = "Array"),
            @ApiImplicitParam(name = "opus_jurisdiction", value = "作品公开范围，1所有人，2好友，3不公开", required = true, dataType = "Number"),
            @ApiImplicitParam(name = "opus_original", value = "作品是否原创 ，1原创，0非原创", required = true, dataType = "Number"),
            @ApiImplicitParam(name = "opus_time", value = "作品指定发布时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "opus_syncDynamic", value = "前端显示 开关按钮 样式，按钮打开状态：传 true，表示需要同步作品到动态中，否则传 false", required = true, dataType = "Number"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/PushOpus")
    public ResultModel pushOpus(@RequestBody PushOpusModel Opus) {
        return opusService.pushOpus(Opus);
    }



    @UserLoginToken
    @ApiOperation(value="【需要Token】检查作品是否被收藏", notes="用于作品详情页，检查作品是否被收藏，true表示点收藏过了，false 表示未被收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "作品的id", required = true, dataType = "Number")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/CheckOpusIsCollection")
    public ResultModel CheckOpusIsCollection(@RequestBody Map<String, Integer> Opus) {
        return opusService.checkOpusIsCollection(Opus.get("opus_id"));
    }

    @UserLoginToken
    @ApiOperation(value="【需要Token】作品的搜索", notes="用于作品相关的检索，数据来源从作品，用户，标签/话题 中来！")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "keyword",
                    value = "搜索关键字，Flutter需要使用 convert.base64Encode(convert.utf8.encode('中文')); 进行Base64编码，然后传递给后端！",
                    required = true,
                    dataType = "String"
            ),
            @ApiImplicitParam(
                    name = "type",
                    value = "搜索的类型，取值：作品 opus，用户 user，标签 tag", required = true, dataType = "String"
            ),
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "String", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "String", defaultValue="15"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/OpusSearch")
    public ResultModel OpusSearch(@RequestBody Map<String, String> Opus) throws Exception {
        // 5aSn6ZuE
        if (Opus.get("page")==null) { Opus.put("page", "1"); }
        if (Opus.get("count")==null) { Opus.put("count", "15"); }

        Opus.put("keyword", Tool.decodeBase64(Opus.get("keyword")));
        return opusService.OpusSearch(Opus);
    }
}
